import 'dart:ffi';
import 'dart:ui';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';


class MyEditableText extends EditableText {

  MyEditableText({
    key,
    required controller,
    required focusNode,
    readOnly = false,
    obscuringCharacter = '•',
    obscureText = false,
    autocorrect = true,
    SmartDashesType? smartDashesType,
    SmartQuotesType? smartQuotesType,
    enableSuggestions = true,
    required style,
    StrutStyle? strutStyle,
    required cursorColor,
    required backgroundCursorColor,
    textAlign = TextAlign.start,
    textDirection,
    locale,
    @Deprecated(
      'Use textScaler instead. '
          'Use of textScaleFactor was deprecated in preparation for the upcoming nonlinear text scaling support. '
          'This feature was deprecated after v3.12.0-2.0.pre.',
    )
    textScaleFactor,
    textScaler,
    maxLines = 1,
    minLines,
    expands = false,
    forceLine = true,
    textHeightBehavior,
    textWidthBasis = TextWidthBasis.parent,
    autofocus = false,
    bool? showCursor,
    showSelectionHandles = false,
    selectionColor,
    selectionControls,
    TextInputType? keyboardType,
    textInputAction,
    textCapitalization = TextCapitalization.none,
    onChanged,
    onEditingComplete,
    onSubmitted,
    onAppPrivateCommand,
    onSelectionChanged,
    onSelectionHandleTapped,
    onTapOutside,
    List<TextInputFormatter>? inputFormatters,
    mouseCursor,
    rendererIgnoresPointer = false,
    cursorWidth = 2.0,
    cursorHeight,
    cursorRadius,
    cursorOpacityAnimates = false,
    cursorOffset,
    paintCursorAboveText = false,
    selectionHeightStyle = BoxHeightStyle.tight,
    selectionWidthStyle = BoxWidthStyle.tight,
    scrollPadding = const EdgeInsets.all(20.0),
    keyboardAppearance = Brightness.light,
    dragStartBehavior = DragStartBehavior.start,
    bool? enableInteractiveSelection,
    scrollController,
    scrollPhysics,
    autocorrectionTextRectColor,
    @Deprecated(
      'Use `contextMenuBuilder` instead. '
          'This feature was deprecated after v3.3.0-0.5.pre.',
    )
    ToolbarOptions? toolbarOptions,
    autofillHints = const <String>[],
    autofillClient,
    clipBehavior = Clip.hardEdge,
    restorationId,
    scrollBehavior,
    scribbleEnabled = true,
    enableIMEPersonalizedLearning = true,
    contentInsertionConfiguration,
    contextMenuBuilder,
    spellCheckConfiguration,
    magnifierConfiguration = TextMagnifierConfiguration.disabled,
    undoController,
  }):super (
    key: key,
    controller: controller,
    focusNode:focusNode,
    readOnly: readOnly,
    obscuringCharacter: obscuringCharacter,
    obscureText: obscureText,
    autocorrect: autocorrect,
    smartDashesType: smartDashesType,
    smartQuotesType: smartQuotesType,
    enableSuggestions: enableSuggestions,
    style: style,
    strutStyle: strutStyle,
    cursorColor: cursorColor,
    backgroundCursorColor: backgroundCursorColor,
    textAlign: textAlign,
    textDirection: textDirection,
    locale: locale,
    textScaleFactor: locale,
    textScaler: textScaler,
    maxLines: maxLines,
    minLines: minLines,
    expands: expands,
    forceLine: forceLine,
    textHeightBehavior: textHeightBehavior,
    textWidthBasis: textWidthBasis,
    autofocus: autofocus,
    showCursor: showCursor,
    showSelectionHandles: showSelectionHandles,
    selectionColor: selectionColor,
    selectionControls: selectionControls,
    keyboardType: keyboardType,
    textInputAction: textInputAction,
    textCapitalization: textCapitalization,
    onChanged: onChanged,
    onEditingComplete: onEditingComplete,
    onSubmitted: onSubmitted,
    onAppPrivateCommand: onAppPrivateCommand,
    onSelectionChanged: onSelectionChanged,
    onSelectionHandleTapped: onSelectionHandleTapped,
    onTapOutside: onTapOutside,
    inputFormatters: inputFormatters,
    mouseCursor: mouseCursor,
    rendererIgnoresPointer: rendererIgnoresPointer,
    cursorWidth: cursorWidth,
    cursorHeight: cursorHeight,
    cursorRadius: cursorRadius,
    cursorOpacityAnimates: cursorOpacityAnimates,
    cursorOffset: cursorOffset,
    paintCursorAboveText: paintCursorAboveText,
    selectionHeightStyle: selectionHeightStyle,
    selectionWidthStyle: selectionWidthStyle,
    scrollPadding: scrollPadding,
    keyboardAppearance: keyboardAppearance,
    dragStartBehavior: dragStartBehavior,
    enableInteractiveSelection: enableInteractiveSelection,
    scrollController: scrollController,
    scrollPhysics: scrollPhysics,
    autocorrectionTextRectColor: autocorrectionTextRectColor,
    toolbarOptions: toolbarOptions,
    autofillHints: autofillHints,
    autofillClient: autofillClient,
    clipBehavior: clipBehavior,
    restorationId: restorationId,
    scrollBehavior: scrollBehavior,
    scribbleEnabled: scribbleEnabled,
    enableIMEPersonalizedLearning: enableIMEPersonalizedLearning,
    contentInsertionConfiguration: contentInsertionConfiguration,
    contextMenuBuilder: contextMenuBuilder,
    spellCheckConfiguration: spellCheckConfiguration,
    magnifierConfiguration: magnifierConfiguration,
    undoController: undoController,
  );

  @override
  EditableTextState createState() {
    return MyEditableTextSpan();
  }


}

class MyEditableTextSpan extends EditableTextState {

  @override
  TextSpan buildTextSpan() {
    var widget = super.widget;
    print(widget.controller.text);

    // widget.controller.text = "${widget.controller.text}hi";

    TextSpan textSpan = TextSpan(
      style: widget.style,
      children: [
        TextSpan(
          text: widget.controller.text.length > 100 ? widget.controller.text.substring(0, 5) : widget.controller.text,
          style: TextStyle(fontStyle: FontStyle.italic),
          // recognizer: TapGestureRecognizer()..onTap = () {
          //   print("onTap");
          // }
          // recognizer: TapGestureRecognizer()..onTap = () {
          //   if (widget.controller.text.isNotEmpty) {
          //     final url = widget.controller.text.split(' ').last;
          //     if (url.startsWith('http')) {
          //       print(url);
          //     }
          //   }
          // },
        ),
        // TextSpan(
        //   text: widget.controller.text.length > 5 ? widget.controller.text.substring(5, widget.controller.text.length) : widget.controller.text.substring(0, 0),
        //   style: TextStyle(
        //       fontStyle: FontStyle.normal,
        //       fontWeight: FontWeight.bold,
        //   ),
        //
        // ),
        // WidgetSpan(
        //   child: Image.network("https://avatars2.githubusercontent.com/u/20411648?s=460&v=4", width: 100.0)
        // )
        // WidgetSpan(
        //     child: IconButton(
        //       icon: Icon(Icons.edit),
        //       onPressed: () {
        //         print("---");
        //       },
        //     )
        // ),
      ],
    );

    return textSpan;
    // return widget.controller.buildTextSpan(
    //   context: context,
    //   style: widget.style,
    //   withComposing: true,
    // );
  }
}
